{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 训练集选择10000个样本，测试集选择1000个样本"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_sample = 1000\n",
    "csv_file_path = 'data/test.csv'\n",
    "save_path = 'data/test.json'\n",
    "df = pd.read_csv(csv_file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>pixel1</th>\n",
       "      <th>pixel2</th>\n",
       "      <th>pixel3</th>\n",
       "      <th>pixel4</th>\n",
       "      <th>pixel5</th>\n",
       "      <th>pixel6</th>\n",
       "      <th>pixel7</th>\n",
       "      <th>pixel8</th>\n",
       "      <th>pixel9</th>\n",
       "      <th>...</th>\n",
       "      <th>pixel1016</th>\n",
       "      <th>pixel1017</th>\n",
       "      <th>pixel1018</th>\n",
       "      <th>pixel1019</th>\n",
       "      <th>pixel1020</th>\n",
       "      <th>pixel1021</th>\n",
       "      <th>pixel1022</th>\n",
       "      <th>pixel1023</th>\n",
       "      <th>pixel1024</th>\n",
       "      <th>hash</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>199</td>\n",
       "      <td>51</td>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>227</td>\n",
       "      <td>255</td>\n",
       "      <td>127</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>24ced6eb4c3b6b16e6d911401ca9b9bf</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>24</td>\n",
       "      <td>56</td>\n",
       "      <td>93</td>\n",
       "      <td>220</td>\n",
       "      <td>101</td>\n",
       "      <td>80</td>\n",
       "      <td>...</td>\n",
       "      <td>72</td>\n",
       "      <td>142</td>\n",
       "      <td>247</td>\n",
       "      <td>1</td>\n",
       "      <td>247</td>\n",
       "      <td>27</td>\n",
       "      <td>121</td>\n",
       "      <td>109</td>\n",
       "      <td>75</td>\n",
       "      <td>9c9133f89cf857325fd041ca8640d51b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>9</td>\n",
       "      <td>116</td>\n",
       "      <td>139</td>\n",
       "      <td>255</td>\n",
       "      <td>244</td>\n",
       "      <td>252</td>\n",
       "      <td>137</td>\n",
       "      <td>139</td>\n",
       "      <td>139</td>\n",
       "      <td>255</td>\n",
       "      <td>...</td>\n",
       "      <td>108</td>\n",
       "      <td>73</td>\n",
       "      <td>3</td>\n",
       "      <td>105</td>\n",
       "      <td>85</td>\n",
       "      <td>183</td>\n",
       "      <td>102</td>\n",
       "      <td>171</td>\n",
       "      <td>7</td>\n",
       "      <td>13544f465094166da9ad6f04833c4a7f</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 1026 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   label  pixel1  pixel2  pixel3  pixel4  pixel5  pixel6  pixel7  pixel8  \\\n",
       "0      2       0     199      51      23       0       0     227     255   \n",
       "1      6       0       0       4      24      56      93     220     101   \n",
       "2      9     116     139     255     244     252     137     139     139   \n",
       "\n",
       "   pixel9  ...  pixel1016  pixel1017  pixel1018  pixel1019  pixel1020  \\\n",
       "0     127  ...          0          0          0          0          0   \n",
       "1      80  ...         72        142        247          1        247   \n",
       "2     255  ...        108         73          3        105         85   \n",
       "\n",
       "   pixel1021  pixel1022  pixel1023  pixel1024  \\\n",
       "0          0          0          0          0   \n",
       "1         27        121        109         75   \n",
       "2        183        102        171          7   \n",
       "\n",
       "                               hash  \n",
       "0  24ced6eb4c3b6b16e6d911401ca9b9bf  \n",
       "1  9c9133f89cf857325fd041ca8640d51b  \n",
       "2  13544f465094166da9ad6f04833c4a7f  \n",
       "\n",
       "[3 rows x 1026 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = []\n",
    "for i in range(num_sample):\n",
    "    img = df.iloc[i,1:1+1024].to_list()\n",
    "    img = [int(pixel) for pixel in img]\n",
    "    label = int(df.iloc[i,0])\n",
    "    data.append([img, label])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(save_path, 'w', encoding='utf8') as f:\n",
    "    json.dump(data, f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取训练集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_sample = 10000\n",
    "csv_file_path = 'data/train.csv'\n",
    "save_path = 'data/train.json'\n",
    "df = pd.read_csv(csv_file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>pixel1</th>\n",
       "      <th>pixel2</th>\n",
       "      <th>pixel3</th>\n",
       "      <th>pixel4</th>\n",
       "      <th>pixel5</th>\n",
       "      <th>pixel6</th>\n",
       "      <th>pixel7</th>\n",
       "      <th>pixel8</th>\n",
       "      <th>pixel9</th>\n",
       "      <th>...</th>\n",
       "      <th>pixel1016</th>\n",
       "      <th>pixel1017</th>\n",
       "      <th>pixel1018</th>\n",
       "      <th>pixel1019</th>\n",
       "      <th>pixel1020</th>\n",
       "      <th>pixel1021</th>\n",
       "      <th>pixel1022</th>\n",
       "      <th>pixel1023</th>\n",
       "      <th>pixel1024</th>\n",
       "      <th>hash</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>212</td>\n",
       "      <td>1</td>\n",
       "      <td>77</td>\n",
       "      <td>204</td>\n",
       "      <td>137</td>\n",
       "      <td>117</td>\n",
       "      <td>139</td>\n",
       "      <td>161</td>\n",
       "      <td>199</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>aa527b5aea9fe96fabae78e85ba59491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>3</td>\n",
       "      <td>233</td>\n",
       "      <td>127</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>233</td>\n",
       "      <td>...</td>\n",
       "      <td>104</td>\n",
       "      <td>28</td>\n",
       "      <td>233</td>\n",
       "      <td>150</td>\n",
       "      <td>115</td>\n",
       "      <td>173</td>\n",
       "      <td>126</td>\n",
       "      <td>27</td>\n",
       "      <td>64</td>\n",
       "      <td>05597db8265479234c8e15a7290cd7f0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>181</td>\n",
       "      <td>232</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>141</td>\n",
       "      <td>133</td>\n",
       "      <td>255</td>\n",
       "      <td>221</td>\n",
       "      <td>...</td>\n",
       "      <td>116</td>\n",
       "      <td>223</td>\n",
       "      <td>203</td>\n",
       "      <td>183</td>\n",
       "      <td>56</td>\n",
       "      <td>59</td>\n",
       "      <td>62</td>\n",
       "      <td>50</td>\n",
       "      <td>51</td>\n",
       "      <td>b1375b03e66c56e9a733eaf3d2a5bdbd</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 1026 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   label  pixel1  pixel2  pixel3  pixel4  pixel5  pixel6  pixel7  pixel8  \\\n",
       "0      3     212       1      77     204     137     117     139     161   \n",
       "1      0       0      19       3     233     127       0      70       1   \n",
       "2      1       0     181     232       0       0     141     133     255   \n",
       "\n",
       "   pixel9  ...  pixel1016  pixel1017  pixel1018  pixel1019  pixel1020  \\\n",
       "0     199  ...          0          0          0          0          0   \n",
       "1     233  ...        104         28        233        150        115   \n",
       "2     221  ...        116        223        203        183         56   \n",
       "\n",
       "   pixel1021  pixel1022  pixel1023  pixel1024  \\\n",
       "0          0          0          0          0   \n",
       "1        173        126         27         64   \n",
       "2         59         62         50         51   \n",
       "\n",
       "                               hash  \n",
       "0  aa527b5aea9fe96fabae78e85ba59491  \n",
       "1  05597db8265479234c8e15a7290cd7f0  \n",
       "2  b1375b03e66c56e9a733eaf3d2a5bdbd  \n",
       "\n",
       "[3 rows x 1026 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = []\n",
    "for i in range(num_sample):\n",
    "    img = df.iloc[i,1:1+1024].to_list()\n",
    "    img = [int(pixel) for pixel in img]\n",
    "    label = int(df.iloc[i,0])\n",
    "    data.append([img, label])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(save_path, 'w', encoding='utf8') as f:\n",
    "    json.dump(data, f)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "96425d339b0e6107dbb91cb157c2db64734def97b9748c3a4592256fc09ea454"
  },
  "kernelspec": {
   "display_name": "Python 3.6.13 ('pypots')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
